﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.Objects;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace QLBN
{
	public partial class FrmMain : Form
	{
		public BenhNhan BenhNhan { get; set; }

		public History History { get; set; }

		public FrmMain()
		{
			InitializeComponent();
			BenhNhan = new BenhNhan();
			benhNhanBindingSource.DataSource = BenhNhan;
		}

		private void btnSave_Click(object sender, EventArgs e)
		{
			using (DBEntities context = new DBEntities() )
			{
                try
                {
                    if(string.IsNullOrEmpty(txtName.Text))
                    {
                        MessageBox.Show("Xin vui lòng nhập tên bệnh nhân", "Chú ý", MessageBoxButtons.OK);
                        return;
                    }

                    if (BenhNhan.ID == Guid.Empty)
                    {
                        BenhNhan.ID = Guid.NewGuid();
                        context.BenhNhans.AddObject(BenhNhan);
                        newHistory();
                    }
                    else
                    {
                        context.BenhNhans.Attach(BenhNhan);
                        context.ObjectStateManager.ChangeObjectState(BenhNhan, EntityState.Modified);
                    }
                    context.SaveChanges();
                    lblStatus.Text = "Lưu thành công";
                    groupKhamBenh.Enabled = true;
                }catch(Exception ex)
                {
                    lblStatus.Text = "Đã có lỗi xảy ra...";
                }
			}
		}

		private void BtnNew_Click(object sender, EventArgs e)
		{
			BenhNhan = new BenhNhan();
			benhNhanBindingSource.DataSource = BenhNhan;
			nAge.Value = 0;
		    cmbHistory.DataSource = null;
            History = null;
		    groupKhamBenh.Enabled = false;
		    lblStatus.Text = "";
		    txtName.Focus();
		}

		private void btnSearch_Click(object sender, EventArgs e)
		{
			FrmSearch search = new FrmSearch();
			search.FrmMain = this;
			search.ShowDialog();
		}

		public void SetData(BenhNhan bn)
		{
			BenhNhan = bn;
			benhNhanBindingSource.DataSource = BenhNhan;
			nAge.Value = BenhNhan.Tuoi != null ? BenhNhan.Tuoi.Value : 0;
		    lblStatus.Text = "";
            if (BenhNhan.Histories.Any())
            {
                initHistories();
            }
            else
            {
                newHistory();
            }
		    groupKhamBenh.Enabled = true;
		}

        private void cmbHistory_SelectionChangeCommitted(object sender, EventArgs e)
        {
            if (cmbHistory.SelectedIndex != -1)
            {
                History = (History) cmbHistory.SelectedItem;
                historyBindingSource.DataSource = History;
            }
        }

        private void btnSaveHistory_Click(object sender, EventArgs e)
        {
            using (DBEntities context = new DBEntities())
            {
                try
                {
                    if (History.ID == Guid.Empty)
                    {
                        History.ID = Guid.NewGuid();
                        History.Date = DateTime.Now;
                        context.Histories.AddObject(History);
                    }
                    else
                    {
                        context.Histories.Attach(History);
                        context.ObjectStateManager.ChangeObjectState(History, EntityState.Modified);
                    }
                    History.BNID = BenhNhan.ID;
                    context.SaveChanges();
                    initHistories();
                    lblStatus.Text = "Đã lưu thành công thông tinh khám bệnh!";
                }
                catch (Exception ex)
                {
                    lblStatus.Text = "Lưu thất bại. Đã có lỗi xảy ra...";
                }
            }
        }

        private void btnNewHistory_Click(object sender, EventArgs e)
        {
            newHistory();
        }

        private void initHistories()
        {
            using (DBEntities context = new DBEntities())
            {
                var histories = context.Histories.Where(h => h.BNID == BenhNhan.ID).OrderByDescending(h => h.Date).ToList();
                cmbHistory.DataSource = histories;
                cmbHistory.DisplayMember = "Date";
                cmbHistory.ValueMember = "ID";
                History = histories[0];
                historyBindingSource.DataSource = History;
            }
        }

        private void newHistory()
        {
            History = new History();
            historyBindingSource.DataSource = History;
            txtChuanDoan.Focus();
        }

        private void btnHuy_Click(object sender, EventArgs e)
        {
            initHistories();
        }
	}
}
